package com.surfilter.platform.data.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.surfilter.platform.base.exception.CustomException;
import com.surfilter.platform.base.exception.SystemException;
import com.surfilter.platform.base.model.ActionPermission;
import com.surfilter.platform.base.model.Constants;
import com.surfilter.platform.base.model.PermissionMethodType;
import com.surfilter.platform.base.model.ResultCode;
import com.surfilter.platform.base.service.ServiceBase;
import com.surfilter.platform.base.service.ServiceContext;
import com.surfilter.platform.base.service.SpringContextUtil;
import com.surfilter.platform.base.util.MD5;
import com.surfilter.platform.base.util.ResultDataUtil;
import com.surfilter.platform.data.dao.InitDao;
import com.surfilter.platform.data.model.ModuleModel;
import com.surfilter.platform.data.model.UserModel;

@Service(value = Constants.SERVICE_PLATFORM)
public class PlatformServiceImpl extends ServiceBase{

	@Resource
	private InitDao initDao;
	
	@ActionPermission(actionName = "login",remark = "用户登陆", permission = PermissionMethodType.NONE)
	public static UserModel login(HttpServletRequest request,
			HttpServletResponse response) {
			String verifyCode = request.getParameter("verifyCode");
			HttpSession session = request.getSession(true);
			String sessionVerifyCode = (String) session
					.getAttribute("validateCode");
			if (verifyCode != null
					&& verifyCode.equalsIgnoreCase(sessionVerifyCode)) {

				String userName = request.getParameter("userName");
				String password = request.getParameter("password");

				if (userName == null || password == null
						|| userName.trim().length() == 0
						|| password.trim().length() == 0) {
					throw new CustomException("用户名或密码不能为空");
				}

				String md5 = MD5.encode(password);

				UserModel loginUserModel = new UserModel();
				loginUserModel.setUserName(userName);
				loginUserModel.setPassword(md5);

				InitDao initDao = (InitDao) SpringContextUtil
						.getBean(InitDao.class);

				if (initDao==null) {
					throw new SystemException("InitDao Not Found");
				}

				UserModel userModel = initDao.getUserByLogin(loginUserModel);

				if (userModel == null) {
					throw new CustomException("用户名或密码错误");
				} else {
					session.setAttribute(Constants.LOGIN_USER_TAG, userModel);

					ServiceContext.setSessionPermission(session, userModel);

					String resultData = ResultDataUtil.wrapResultData(true,
							ResultCode.SUCCESS, "登陆成功", userModel, null);
					return userModel;
				}
			} else {
				throw new CustomException("验证码错误");
			}
	}
	
	@ActionPermission(actionName = "logout", remark = "用户登出", permission = PermissionMethodType.NONE)
	public static void logout(HttpServletRequest request,
			HttpServletResponse response){
			request.getSession(true).invalidate();
			String resultData = ResultDataUtil.wrapResultData(true,
					ResultCode.SUCCESS, "success", null, null);
	}
	
	@ActionPermission(actionName = "getUserInitPermission",remark = "获取用户初始权限",  permission = PermissionMethodType.NONE)
	public List<String> getUserInitPermission(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session == null
				|| session.getAttribute(Constants.LOGIN_USER_TAG) == null) {
			throw new CustomException("用户未登陆");
		} else {
			UserModel userModel = (UserModel) session.getAttribute(Constants.LOGIN_USER_TAG);
			ServiceContext.setSessionPermission(session, userModel);
			Set<String> perActions = (Set<String>) session
					.getAttribute(Constants.SESSION_ACTIONS_TAG);
			return new ArrayList(perActions);
		}
	}

	@ActionPermission(actionName = "getUserPerModules",remark = "获取用户模块", permission = PermissionMethodType.NONE)
	public List<ModuleModel> getUserPerModules(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session == null
				|| session.getAttribute(Constants.LOGIN_USER_TAG) == null) {
			throw new CustomException("用户未登陆");
		} else {
			UserModel user = (UserModel) session
					.getAttribute(Constants.LOGIN_USER_TAG);
			List<ModuleModel> modules = initDao.getUserPerModules(
					user.getId());
			return modules;
		}
	}
	
	@ActionPermission(actionName = "checkLogin", remark = "检验登陆", permission = PermissionMethodType.NONE, skipLogging = true)
	public static UserModel checkLogin(HttpServletRequest request,
			HttpServletResponse response)  {
			UserModel user = null;
			HttpSession session = request.getSession();
			if (session != null) {
				user = (UserModel) session
						.getAttribute(Constants.LOGIN_USER_TAG);
			}
			return user;
	}
	
}
